//
//  RMMOrderViewController.m
//  Romomo
//
//  Created by puman on 16/9/30.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMOrderViewController.h"
#import "RMMOrdHeadView.h"
#import "RMMOrderEmptyView.h"
#import "RMMOrderTableViewCell.h"
#import "RMMTuiKuanViewController.h"
#import "RMMOrderDetailsViewController.h"
#import "RMMGoodsConfirmViewController.h"
#import "RMMSelcetPickerView.h"
#import "RMMTuiKuanViewController.h"
#import "RMMAddBookingOrderViewController.h"
#import "RMMShopingModel.h"
#import "RMMOrderEvaluationViewController.h"
#import "RMMTaoCanDetailsViewController.h"
#import "RMMXiuGaiOrderView.h"
#import "RMMChosseDateViewController.h"
#import "RMMChooseSkillViewController.h"
#import "RMMLongingViewController.h"

@interface RMMOrderViewController ()<UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource, RMMXiuGaiOrderViewDelegate>


@end

@implementation RMMOrderViewController
{
    RMMOrdHeadView *_headView;
    RMMXiuGaiOrderView *_xiugaiView;
    RMMOrderEmptyView *_emptyView;
    RMMSelcetPickerView *_pickerView;
    NSMutableDictionary *_dataDic;
    UITableView *_tableView;
    NSString *_order_no;
    NSInteger _buttonTag;
    NSArray *_keyArray;
    NSArray *_array;
    NSString *_key;
    NSString *_cancelremak;
    BOOL _isAddFresh;
    NSString *_isHome;
    NSString *_order_id;
    RMMOrderModel *_model;
}
- (void)dealloc{
    NS_REM(@"ORDERUPDATA", nil);
    NS_REM(@"TUICHUDENGLU", nil);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    _dataDic = [NSMutableDictionary dictionary];
    _isAddFresh = NO;
    [self addViews];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[RMMFileManager shareHandle].bookingDic removeAllObjects];
    if ([RMMFileManager shareHandle].isLoginSuccess) {
        if (!_isAddFresh) {
            _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self getRequestData];
            }];
            _isAddFresh = YES;
        }
        if (_dataDic != nil && ![_dataDic allKeys].count) {
            [self getRequestData];
        }
    } else {
        [self addLoginAlertView];
    }
}
- (void)addLoginAlertView{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"查看订单,需要您先前往登录" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"前往" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        RMMLongingViewController *loginVC = [[RMMLongingViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
        
    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:actionA];
    [alertController addAction:actionB];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)addViews{
    _buttonTag = 10;
    NS_ADD(tuiChuDengLu, @"TUICHUDENGLU", nil);
    _keyArray = @[@"进行中",@"待评价",@"已完成",@"已取消"];
    _array = @[@"忘记用优惠券喽",@"突然有事很不好意思",@"重新选定项目",@"需要换个美容师",@"手一哆嗦多拍了"];
    _key = _keyArray[_buttonTag - 10];
    _headView = [[RMMOrdHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth,kHalf - kRow)];
    [self.view addSubview:_headView];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [self.view viewWithTag:10 + i];
        BUTTON_S(button, buttonClick:);
    }
    [self addTableView];
    NS_ADD(freshData, @"ORDERUPDATA", nil);
}
- (void)freshData{
    [self getRequestData];
}
- (void)getRequestData{
    NSString *cust_id = [RMMFileManager retrunObjWith:[RMMFileManager shareHandle].userDic[@"CUST_ID"]];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"cust_id"] = cust_id;
    NSString *url = GetMyPayOrdersUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            [_tableView.mj_header endRefreshing];
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [_dataDic removeAllObjects];
            NSArray *array = dataDic[@"data"];
            if (!array.count) {
                _tableView.hidden = YES;
                _emptyView.hidden = NO;
                return ;
            } else {
                _tableView.hidden = NO;
                _emptyView.hidden = YES;
            }
            for (NSDictionary *dic in array) {
                RMMOrderModel *model = [[RMMOrderModel alloc] init];
                [model setValuesForKeysWithDictionary:[RMMFileManager setValueForDic:dic]];
                NSArray *bArray = dic[@"BOOK_DETAIL"];
                if (bArray.count) {
                    NSInteger timeLen = 0;
                    for (NSDictionary *subDic in bArray) {
                        NSInteger t = [[RMMFileManager retrunObjWith:[RMMFileManager setValueForDic:subDic][@"TIME_LEN"]] integerValue];
                        timeLen += t;
                    }
                    model.TIME_LEN = [NSString stringWithFormat:@"%ld",timeLen];
                } else {
                    model.TIME_LEN = @"0";
                }
                if ([model.ORDER_STATUS isEqualToString:@"0"] || [model.ORDER_STATUS isEqualToString:@"1"]) {
                    if (![model.BOOK_STATUS isEqualToString:@"4"] && ![model.BOOK_STATUS isEqualToString:@"5"] && ![model.BOOK_STATUS isEqualToString:@"9"] && ![model.BOOK_STATUS isEqualToString:@"8"] && ![model.BOOK_STATUS isEqualToString:@"10"]) {
                        // 进行中
                        NSMutableArray *value = _dataDic[@"进行中"];
                        if (value == nil) {
                            value = [NSMutableArray array];
                            [_dataDic setObject:value forKey:@"进行中"];
                        }
                        [value addObject:model];
                    } if ([model.BOOK_STATUS isEqualToString:@"4"]) {
                        //待评价
                        NSMutableArray *value = _dataDic[@"待评价"];
                        if (value == nil) {
                            value = [NSMutableArray array];
                            [_dataDic setObject:value forKey:@"待评价"];
                        }
                        [value addObject:model];
                    } else if ([model.BOOK_STATUS isEqualToString:@"5"]){
                        // 已完成
                        NSMutableArray *value = _dataDic[@"已完成"];
                        if (value == nil) {
                            value = [NSMutableArray array];
                            [_dataDic setObject:value forKey:@"已完成"];
                        }
                        [value addObject:model];
                    }
                } else {
                    // 已取消
                    NSMutableArray *value = _dataDic[@"已取消"];
                    if (value == nil) {
                        value = [NSMutableArray array];
                        [_dataDic setObject:value forKey:@"已取消"];
                    }
                    [value addObject:model];
                }
            }
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        dispatch_group_t group = dispatch_group_create();
        NSArray *keyArray = [_dataDic allKeys];
        for (int i = 0; i < keyArray.count; i++) {
            NSMutableArray *valueArray = [_dataDic objectForKey:keyArray[i]];
            [self selectSortArrayWith:[_dataDic objectForKey:keyArray[i]] group:group];
           // [self quickSortAarrayWith:[_dataDic objectForKey:keyArray[i]] index:0 andOtherIndex:valueArray.count - 1 group:group];
        }
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            [_tableView reloadData];
            [_tableView.mj_header endRefreshing];
        });
    } error:^(NSError *errors) {
        [_tableView.mj_header endRefreshing];
    } view:self.view];
}
// 冒泡排序呢
- (void)sortArrayWith:(NSMutableArray *)array group:(dispatch_group_t)group{
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        for (int i = 0; i < array.count - 1; i++) {
            for (int j = 0; j < array.count - 1 - i; j++) {
                RMMOrderModel *modelA = array[j];
                RMMOrderModel *modelB = array[j + 1];
                if ([modelA.ORDER_ID floatValue] < [modelB.ORDER_ID floatValue]) {
                    [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                }
            }
        }
    });
}
// 选择排序能省一半时间
- (void)selectSortArrayWith:(NSMutableArray *)array group:(dispatch_group_t)group{
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        for (int i = 0; i < array.count - 1; i++) {
            int maxIndex = i;
            for (int j = i+ 1; j < array.count; j++) {
                RMMOrderModel *modelA = array[j];
                RMMOrderModel *modelB = array[maxIndex];
                if ([modelA.ORDER_ID floatValue] > [modelB.ORDER_ID floatValue]) {
                    maxIndex = j;
                }
            }
            if (i != maxIndex) {
                [array exchangeObjectAtIndex:i withObjectAtIndex:maxIndex];
            }
        }
    });
}
// 二元选择排序
- (void)binarySortArrayWith:(NSMutableArray *)array group:(dispatch_group_t)group{
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        NSInteger time = 0;
        for (int i = 0; i < array.count / 2; i++) {
            int minIndex = array.count - 1- i;
            int maxIndex = i;
            for (int j = i; j < array.count - i; j++) {
                RMMOrderModel *modelA = array[j];
                RMMOrderModel *modelB = array[minIndex];
                RMMOrderModel *modelC = array[maxIndex];
                if ([modelB.ORDER_ID floatValue] > [modelA.ORDER_ID floatValue]) {
                    minIndex = j;
                    time++;
                }
                if ([modelC.ORDER_ID floatValue] < [modelA.ORDER_ID floatValue]) {
                    maxIndex = j;
                    time++;
                }
            }
            if (maxIndex != i) {
                [array exchangeObjectAtIndex:minIndex withObjectAtIndex:i];
                time++;
            }
            if (minIndex != array.count - 1 - i) {
                if (i == array.count / 2 - 1) {
                    break;
                }
                [array exchangeObjectAtIndex:maxIndex withObjectAtIndex:i];
                time++;
            }
        }
        NSLog(@"实际走的次数%ld",time);
    });
}
// 快速排序
- (void)quickSortAarrayWith:(NSMutableArray *)array index:(NSInteger)index andOtherIndex:(NSInteger)otherIndex group:(dispatch_group_t)group{
    dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
        if (index >= otherIndex) {
            return;
        }
        NSInteger i = index;
        NSInteger j = otherIndex;
        RMMOrderModel *modelA = array[index];
        while (i < j) {
            RMMOrderModel *modelB = array[j];
            while (i < j && [modelA.ORDER_ID floatValue ] <= [modelB.ORDER_ID floatValue]) {
                j--;
            }
            [array exchangeObjectAtIndex:i withObjectAtIndex:j];
            RMMOrderModel *modelC = array[j];
            while (i < j && [modelA.ORDER_ID floatValue] >= [modelC.ORDER_ID floatValue]) {
                i++;
            }
            [array exchangeObjectAtIndex:j withObjectAtIndex:i];
        }
        [array replaceObjectAtIndex:i withObject:modelA];
        [self quickSortAarrayWith:array index:index andOtherIndex:i - 1 group:group];
        [self quickSortAarrayWith:array index:j + 1 andOtherIndex:otherIndex group:group];
 
    });
    
}
// 退出登录
- (void)tuiChuDengLu{
    [_dataDic removeAllObjects];
    [_tableView reloadData];
}
- (void)addTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,_headView.bottom, kScreenWideth, kScreenHeight - _headView.bottom - 64 - 49) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = NO;
    [_tableView registerClass:[RMMOrderTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    _pickerView = [[RMMSelcetPickerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64 - 49)];
    _pickerView.pickerView.delegate = self;
    _pickerView.pickerView.dataSource = self;
    BUTTON_S(_pickerView.rihthBut, doneButClick);
    _pickerView.type = @"location";
    _pickerView.hidden = YES;
    [self.view addSubview:_pickerView];
    
    _xiugaiView = [[RMMXiuGaiOrderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kScreenHeight - 64 - 49)];
    _xiugaiView.hidden = YES;
    _xiugaiView.xiuGaiOrderDelegate = self;
    [self.view addSubview:_xiugaiView];

    [self addemptyView];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *value = _dataDic[_key];
    return value.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *array = _dataDic[_key];
    RMMOrderModel *model = array[indexPath.row];
    return 130 + 80.0 * model.BOOK_DETAIL.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RMMOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableArray *array = _dataDic[_key];
    cell.model = array[indexPath.row];
    cell.leftBut.tag = _buttonTag * 10000 + indexPath.row;
    cell.rightBut.tag = _buttonTag * 10000 + indexPath.row + 5000;
    BUTTON_S(cell.leftBut, leftButClick:);
    BUTTON_S(cell.rightBut, rightButClick:);
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RMMOrderDetailsViewController *VC = [[RMMOrderDetailsViewController alloc] init];
    NSMutableArray *array = _dataDic[_key];
    RMMOrderModel *model = array[indexPath.row];
    VC.order_id = model.ORDER_ID;
    VC.order_no = model.ORDER_NO;
    VC.model = model;
    VC.type  = [NSString stringWithFormat:@"%ld",indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)doneButClick{
    [UIView animateWithDuration:kInterval animations:^{
        _pickerView.view.frame = CGRectMake(0, _pickerView.bottom, kScreenWideth, 249);
        _pickerView.backView.alpha = 0.0;
    } completion:^(BOOL finished) {
        _pickerView.hidden = YES;
        [self cancelOrderWith:_cancelremak];
    }];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _array.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _array[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _cancelremak = _array[row];
}
- (void)addemptyView{
    _emptyView = [[RMMOrderEmptyView alloc] initWithFrame:CGRectMake(0, _headView.bottom, kScreenWideth, kScreenHeight - _headView.bottom - 64)];
    _emptyView.hidden = YES;
    BUTTON_S(_emptyView.button, nowGoToOrder);
    [self.view addSubview:_emptyView];
    
}
- (void)buttonClick:(UIButton *)button{
    _buttonTag = button.tag;
    _key = _keyArray[_buttonTag - 10];
    for (int i = 0; i < 4; i++) {
        UIButton *buttons = [self.view viewWithTag:10 + i];
        if (buttons.tag == _buttonTag) {
            [buttons setTitleColor:COLORFROMCODE(0x2C2C2C, 1) forState:(UIControlStateNormal)];
            buttons.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
            buttons.userInteractionEnabled = NO;
            
        } else {
            [buttons setTitleColor:COLORFROMCODE(0xBFBFBF, 1) forState:(UIControlStateNormal)];
            buttons.backgroundColor = kWhiteColor;
            buttons.userInteractionEnabled = YES;
        }
    }
    [_tableView reloadData];
}
// 立即下单
- (void)nowGoToOrder{
    UITabBarController *VC = (UITabBarController *)keyWidow.rootViewController;
    [VC setSelectedIndex:0];
}
- (void)rightButClick:(UIButton *)button{
    NSInteger row = (button.tag - 5000) % 10000;
    NSMutableArray *value = _dataDic[_key];
    RMMOrderModel *model = value[row];
    if ([[button titleForState:(UIControlStateNormal)] isEqualToString:@"去支付"]) {
        // 去支付/此处后台定义字段有点乱没法简单的写
        NSArray *array = model.BOOK_DETAIL;
        NSDictionary *dicDetails = array[0];
        NSString *bom_id = [RMMFileManager retrunObjWith:dicDetails[@"BOM_ID"]];
        if (bom_id.length && ![bom_id isEqualToString:@"(null)"]) {
            RMMTaoCanDetailsViewController *VC = [[RMMTaoCanDetailsViewController alloc] init];
            VC.bom_id = bom_id;
            [self.navigationController pushViewController:VC animated:YES];
        } else {
            RMMGoodsConfirmViewController *VC = [[RMMGoodsConfirmViewController alloc] init];
            VC.goodsArray = [NSMutableArray array];
            VC.order_id = [NSString stringWithFormat:@"%@",model.ORDER_ID];
            VC.ORDER_NO = [NSString stringWithFormat:@"%@",model.ORDER_NO];
            for (NSDictionary *dic in array) {
                NSDictionary *dicM = [RMMFileManager setValueForDic:dic];
                RMMShopingModel *models = [[RMMShopingModel alloc] init];
                models.SERVICE_ID = [RMMFileManager retrunObjWith:dicM[@"SERVER_ID"]];
                models.IMAGEURL = dicM[@"SER_IMG_URL"];
                models.SERVICE_NAME = dicM[@"PROD_NAME"];
                models.COUNT = dicM[@"PROD_CNT"];
                models.TIME_LEN = dicM[@"TIME_LEN"];
                models.isSelect = @"1";
                NSString *my_card_id = dicM[@"MY_CARD_ID"];
                models.MY_CARD_ID = my_card_id.length == 0 ? @"0" : my_card_id;
                models.PRICE = [NSString stringWithFormat:@"%@",dic[@"PROD_PRICE"]];
                models.AVA_QTY = [NSNumber numberWithInteger:[dic[@"PROD_CNT"] integerValue]];
                [VC.goodsArray addObject:models];
            }
            VC.isHome = model.IS_HOME;
            [self.navigationController pushViewController:VC animated:YES];
        }
    } else if ([[button titleForState:(UIControlStateNormal)] isEqualToString:@"加单"]) {
        [self GetCanUseTimeWith:model];
    } else if ([[button titleForState:(UIControlStateNormal)] isEqualToString:@"再来一单"]) {
        RMMGoodsConfirmViewController *VC = [[RMMGoodsConfirmViewController alloc] init];
        NSArray *array = model.BOOK_DETAIL;
        NSMutableArray *arrays = [NSMutableArray array];
        NSInteger timeLength = 0;
        for (NSDictionary *dic in array) {
            model.isSelect = @1;
            RMMShopingModel *models = [[RMMShopingModel alloc] init];
            models.SERVICE_ID = [RMMFileManager retrunObjWith:dic[@"SERVER_ID"]];
            models.IMAGEURL = dic[@"SER_IMG_URL"];
            models.SERVICE_NAME = dic[@"PROD_NAME"];
            models.COUNT = dic[@"PROD_CNT"];
            models.TIME_LEN = dic[@"TIME_LEN"];
            models.isSelect = @"1";
            models.MY_CARD_ID = @"0";
            timeLength += [dic[@"TIME_LEN"] integerValue];
            models.PRICE = [NSString stringWithFormat:@"%@",dic[@"PROD_PRICE"]];
            models.AVA_QTY = [NSNumber numberWithInteger:[dic[@"PROD_CNT"] integerValue]];
            [arrays addObject:models];
        }
        VC.isHome = model.IS_HOME;
        VC.goodsArray = arrays;
        [self.navigationController pushViewController:VC animated:YES];
    } else if ([[button titleForState:(UIControlStateNormal)] isEqualToString:@"修改预约"]) {
        if ([model.IS_HOME isEqualToString:@"1"]) {
            NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60 * 60];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *nowTime = [formatter stringFromDate:date];
            if ([nowTime compare:model.BEGIN_DATE] == NSOrderedDescending) {
                [RMMFileManager MBProgressWith:@"技师已出发,不能修改!" onView:self.view];
                return;
            }
        }
        [[RMMFileManager shareHandle].bookingDic setObject:model.BEGIN_DATE forKey:@"时间"];
        if ([model.IS_HOME isEqualToString:@"1"]) {
            [[RMMFileManager shareHandle].bookingDic setObject:@{@"ID":model.ADDRESS_ID} forKey:@"地址"];
        } else {
            [[RMMFileManager shareHandle].bookingDic setObject:@{@"ID":model.STORE_ID} forKey:@"地址"];
        }
        [[RMMFileManager shareHandle].bookingDic setObject:model.ORDER_ID forKey:@"订单ID"];
        NSDictionary *dic = @{@"EMP_NAME":@" ",@"EMP_ID":model.EMP_ID};
       // [[RMMFileManager shareHandle].bookingDic setValue:dic forKey:@"技师"];
        [RMMFileManager shareHandle].totalTimeLength = model.TIME_LEN;
        _isHome = model.IS_HOME;
        _order_id = model.ORDER_ID;
        [self EditorOrder:model.ORDER_ID];
    } else if ([[button titleForState:(UIControlStateNormal)] isEqualToString:@"评价获得颜值分"]) {
        [self gotoEvaluationWith:model];
    }
}
- (void)leftButClick:(UIButton *)button{
    NSString *title = [button titleForState:(UIControlStateNormal)];
    NSInteger row = button.tag % 10000;
    NSMutableArray *value = _dataDic[_key];
    RMMOrderModel *model = value[row];
    if ([title isEqualToString:@"取消订单"]) {
        _order_no = model.ORDER_ID;
        _pickerView.hidden = NO;
        [UIView animateWithDuration:kInterval animations:^{
            _pickerView.view.frame = CGRectMake(0, kScreenHeight - 64 - 249 - 49, kScreenWideth, 249);
            _pickerView.backView.alpha = 1.0;
        } completion:^(BOOL finished) {
        }];
    } else if ([title isEqualToString:@"申请退款"]) {
        RMMTuiKuanViewController *VC = [[RMMTuiKuanViewController alloc] init];
        VC.order_id = model.ORDER_ID;
        VC.type = @"申请退款";
        [self.navigationController pushViewController:VC animated:YES];
    } else if ([title isEqualToString:@"评价获得颜值分"]) {
        [self gotoEvaluationWith:model];
    } else if ([title isEqualToString:@"退款详情"]) {
        RMMTuiKuanViewController *VC = [[RMMTuiKuanViewController alloc] init];
        VC.order_id = model.ORDER_ID;
        VC.type = @"退款中";
        [self.navigationController pushViewController:VC animated:YES];
    }
}
// 修改订单
- (void)EditorOrder:(NSString *)order_id{
    _xiugaiView.hidden = NO;
    [UIView animateWithDuration:kInterval animations:^{
        _xiugaiView.view.frame = CGRectMake(0, _xiugaiView.height - 188, _xiugaiView.width, 188);
        _xiugaiView.backView.alpha = 1.0;
    } completion:^(BOOL finished) {
    }];
}
- (void)touchButCount:(NSInteger)count{
    if (count == 0) {
        // 修改时间
        RMMChosseDateViewController *VC = [[RMMChosseDateViewController alloc] init];
        VC.type = @"2";
        VC.order_id = _order_id;
        VC.isHome = _isHome;
        [self.navigationController pushViewController:VC animated:YES];
        [UIView animateWithDuration:kInterval animations:^{
            _xiugaiView.backView.alpha = 0.0;
             _xiugaiView.view.frame = CGRectMake(0, _xiugaiView.bottom,_xiugaiView.width, 188);
        } completion:^(BOOL finished) {
             _xiugaiView.hidden = YES;
        }];

    } else if (count == 1) {
        // 修改技师
        RMMChooseSkillViewController *VC = [[RMMChooseSkillViewController alloc] init];
        VC.type = @"2";
        VC.isHome = _isHome;
        [self.navigationController pushViewController:VC animated:YES];
        [UIView animateWithDuration:kInterval animations:^{
            _xiugaiView.backView.alpha = 0.0;
            _xiugaiView.view.frame = CGRectMake(0, _xiugaiView.bottom,_xiugaiView.width, 188);
        } completion:^(BOOL finished) {
            _xiugaiView.hidden = YES;
        }];
    }
}
// 评价
- (void)gotoEvaluationWith:(RMMOrderModel *)model{
    RMMOrderEvaluationViewController *VC = [[RMMOrderEvaluationViewController alloc] init];
    VC.model = model;
    [self.navigationController pushViewController:VC animated:YES];
}
// 加单
- (void)GetCanUseTimeWith:(RMMOrderModel *)model{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"emp_id"] = model.EMP_ID;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
    NSDateFormatter *formaatter = [[NSDateFormatter alloc] init];
    [formaatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateTime = [formaatter stringFromDate:date];
    parDic[@"query_time"] = [dateTime compare:model.END_DATE] == NSOrderedAscending ? model.END_DATE  : dateTime;
    NSString *url = GetEmpCanAddOrderTimeUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
             [self addBookingOrderWith:[RMMFileManager retrunObjWith:dataDic[@"data"]] model:model];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
        
    } view:self.view];
}
- (void)addBookingOrderWith:(NSString *)time model:(RMMOrderModel *)model{
    // @"您预约的美容师还有 60 分钟可为您服务,是否要加单?"
    //
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"上门服务加单" message:[NSString stringWithFormat:@"您预约的美容师还有 %@ 分钟可为您服务,是否要加单?",time]  preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        RMMAddBookingOrderViewController *VC = [[RMMAddBookingOrderViewController alloc] init];
        VC.model = model;
        VC.timeL = time;
        VC.isHome = model.IS_HOME;
        [self.navigationController pushViewController:VC animated:YES];
    }];
    [alertController addAction:actionA];
    [alertController addAction:actionB];
    [self presentViewController:alertController animated:YES completion:nil];
}
// 取消订单
- (void)cancelOrderWith:(NSString *)cancelremak{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"order_id"] = _order_no;
    parDic[@"cancelremak"] = cancelremak;
    NSString *url = CancelOrderUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [MMProgressHUD show];
            [MMProgressHUD dismissWithSuccess:@"取消成功!" title:nil afterDelay:1.0];
            [self getRequestData];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
    } view:self.view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
